<template>
    <div class="music-list">
        <div class="header" :style="headerBackground">
            <div class="back" @click="$router.go(-1)">
                <i class="iconfont icon-fanhui"></i>
                <span>{{ headText }}</span>
            </div>
        </div>

        <div class="list" ref="scroll">
            <div class="music-list-wrapper">
                <div>
                    <div class="bg-image" :style="coverImgUrl">
                        <div class="filter"></div>
                        <div class="text">
                            <div class="list-title">{{name}}</div>
                            <p class="update">最近更新:{{updateTime}}</p>
                        </div>
                    </div>
                    <div class="song-list-wrapper">
                        <div class="sequence-play">
                            <i class="iconfont icon-bofang"></i>
                            <span class="text">播放全部</span>
                            <span class="count">(共{{songList.length}})</span>
                        </div>
                        <div class="song-list">
                            <ul>
                                <li
                                    class="item"
                                    v-for="(item , index) in songList"
                                    :key="item.id"
                                    @click="click(item)"
                                >
                                    <p class="count">{{index+1}}</p>
                                    <div class="content">
                                        <h2 class="name">{{item.name}}</h2>
                                        <p class="desc">{{item.singer}}</p>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import BScroll from "@better-scroll/core";
import { mapActions } from "@/store/helper/play.js";
import { getsongList,initialList } from "@/api";
export default {
    name: "songList",
    data() {
        return {
            songList: [],
            name: "",
            coverImgUrl_data: "",
            updateTime_data: "",
            scroll_Y: 0,
            opacity: 0,
            headText: "返回"
        };
    },
    computed: {
        coverImgUrl() {
            return {
                "background-image": "url(" + this.coverImgUrl_data + ")"
            };
        },
        headerBackground() {
            return {
                background: "rgba(212,68,57," + this.opacity + ")"
            };
        },
        updateTime() {
            return this.formatDate(this.updateTime_data, "YY年MM月DD日");
        }
    },
    watch: {
        scroll_Y: function(val) {
            var y = Math.abs(val);
            var h = 100;
            var opacity_data = 0;
            if (y > h) {
                opacity_data = y - h;
            }
            this.opacity = opacity_data / 200;
            if (this.opacity > 1) {
                this.opacity = 1;
            } else if (this.opacity < 0) {
                this.opacity = 0;
            }
        },
        opacity: function(val) {
            if (val === 1) {
                this.headText = this.name;
            } else if (val < 1) {
                this.headText = "返回";
            }
        }
    },
    mounted() {
        let info = {
            type: this.$route.query.type,
            id: this.$route.params.id
        };

        getsongList(info).then(res => {
            var data = res.data.playlist;
            this.name = data.name;
            this.coverImgUrl_data = data.coverImgUrl;
            this.updateTime_data = data.updateTime;
            this.songList = initialList(data.tracks);
            this.init();
        });
    },

    methods: {
        ...mapActions(["selectPlay"]),
        init() {
            this.$nextTick(function() {
                this.bs = new BScroll(this.$refs.scroll, {
                    scrollY: true,
                    click: true,
                    probeType: 3 // listening scroll hook
                });
                this.bs.on("scroll", this.scroll);
            });
        },
        
        // 滚动触发
        scroll(pos) {
            this.scroll_Y = pos.y;
        },
        // 点击列表
        click(song) {
            this.selectPlay({ list: this.songList, song });
        },
        // 时间格式化
        formatDate(time, format = "YY-MM-DD hh:mm:ss") {
            var date = new Date(time);

            var year = date.getFullYear(),
                month = date.getMonth() + 1, //月份是从0开始的
                day = date.getDate(),
                hour = date.getHours(),
                min = date.getMinutes(),
                sec = date.getSeconds();
            var preArr = Array.apply(null, Array(10)).map(function(
                elem,
                index
            ) {
                return "0" + index;
            }); ////开个长度为10的数组 格式为 00 01 02 03

            var newTime = format
                .replace(/YY/g, year)
                .replace(/MM/g, preArr[month] || month)
                .replace(/DD/g, preArr[day] || day)
                .replace(/hh/g, preArr[hour] || hour)
                .replace(/mm/g, preArr[min] || min)
                .replace(/ss/g, preArr[sec] || sec);

            return newTime;
        }
    }
};
</script>

<style lang="less" scoped>
.slide-enter-active,
.slide-leave-active {
    -webkit-transition: all 0.2s;
    transition: all 0.2s;
}
.slide-enter,
.slide-leave-to {
    -webkit-transform: translate3d(30%, 0, 0);
    transform: translate3d(30%, 0, 0);
    opacity: 0;
}
.music-list {
    position: fixed;
    z-index: 100;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: #f2f3f4;
    overflow: hidden;
    height: 100%;
}
.music-list .list {
    height: 100%;
    background: #f2f3f4;
    overflow: hidden;
}
.music-list .header {
    position: fixed;
    top: 0;
    width: 100%;
    height: 44px;
    color: #fff;
    z-index: 100;
}
.music-list .header .back {
    position: absolute;
    top: 0;
    left: 0;
    padding-left: 10px;
    height: 44px;
    line-height: 44px;
}
.music-list .header .back .fa-angle-left {
    padding: 5px 10px;
    font-size: 30px;
}
.music-list .header .text {
    position: absolute;
    left: 38px;
    line-height: 44px;
    font-size: 16px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}

.music-list .list .music-list-wrapper .bg-image {
    position: relative;
    width: 100%;
    height: 0;
    padding-top: 75%;
    -webkit-transform-origin: top;
    transform-origin: top;
    background-size: cover;
    background-position: 0 30%;
}
.music-list .list .music-list-wrapper .bg-image .filter {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: black;
    opacity: 0.2;
}
.music-list .list .music-list-wrapper .bg-image .text {
    position: absolute;
    width: 80%;
    height: 40px;
    bottom: 50px;
    left: 20px;
    color: #fff;
}
.music-list .list .music-list-wrapper .bg-image .text .list-title {
    position: absolute;
    bottom: 0;
    font-style: italic;
    font-size: 18px;
    line-height: 18px;
    font-weight: bold;
    letter-spacing: 1px;
}
.music-list .list .music-list-wrapper .bg-image .text .list-title .music {
    position: absolute;
    top: -20px;
    left: 5px;
    font-style: italic;
    font-weight: bold;
    font-size: 14px;
}
.music-list .list .music-list-wrapper .bg-image .text .update {
    position: absolute;
    top: 45px;
    left: 7px;
    line-height: 14px;
    font-size: 11px;
}
.music-list .list .music-list-wrapper .song-list-wrapper {
    padding: 41px 0 20px 0;
    border-radius: 10px;
    position: relative;
    top: -20px;
    background: #f2f3f4;
}
.music-list .list .music-list-wrapper .song-list-wrapper .sequence-play {
    position: absolute;
    top: 0px;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    width: 100%;
    height: 40px;
    padding-left: 16px;
    border-bottom: 1px solid #e4e4e4;
}
.music-list
    .list
    .music-list-wrapper
    .song-list-wrapper
    .sequence-play
    .iconfont {
    font-size: 18px;
    color: #2e3030;
    padding-right: 14px;
}
.music-list .list .music-list-wrapper .song-list-wrapper .sequence-play .text {
    font-size: 16px;
}
.music-list .list .music-list-wrapper .song-list-wrapper .sequence-play .count {
    font-size: 14px;
    color: #757575;
}
.music-list .loading-content {
    position: fixed;
    width: 100%;
    top: 70%;
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
}
.song-list .item {
    position: relative;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    height: 60px;
    border-bottom: 1px solid #e4e4e4;
}
.song-list .item .count {
    -webkit-box-flex: 0;
    -ms-flex: 0 0 50px;
    flex: 0 0 50px;
    width: 50px;
    text-align: center;
    color: #757575;
}
.song-list .item .content {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    line-height: 20px;
    overflow: hidden;
}
.song-list .item .content .name {
    font-weight: 400;
    font-size: 16px;
    margin-top: 4px;
    width: 80%;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    color: #2e3030;
}
.song-list .item .content .desc {
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    width: 80%;
    font-size: 12px;
    color: #757575;
}
</style>